package com.softer.wxzj.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.softer.wxzj.common.entity.CommonResult;
import com.softer.wxzj.common.entity.Page;
import com.softer.wxzj.common.enums.MessageEnum;
import com.softer.wxzj.common.enums.NumStrEnum;
import com.softer.wxzj.common.enums.NumberEnum;
import com.softer.wxzj.common.enums.ShiroResultEnum;
import com.softer.wxzj.common.error.CourseException;
import com.softer.wxzj.common.flow.enums.FlowTypeEnum;
import com.softer.wxzj.common.flow.util.FlowAddAuditUtil;
import com.softer.wxzj.common.flow.util.FlowAuditUtil;
import com.softer.wxzj.entity.ExpProjectReq;
import com.softer.wxzj.entity.FlowAuditdetail;
import com.softer.wxzj.mapper.BaseOwnerCommitteeMapper;
import com.softer.wxzj.mapper.ExpProjectReqMapper;
import com.softer.wxzj.mapper.FlowAuditdetailMapper;
import com.softer.wxzj.service.ExpProjectReqAuditService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author Gong Zhiyang
 * @date 2019/6/26
 */
@Service
public class ExpProjectReqAuditServiceImpl implements ExpProjectReqAuditService {
    @Autowired
    private FlowAddAuditUtil flowAddAuditUtil;
    @Autowired
    private FlowAuditUtil flowUtil;
    @Autowired
    private ExpProjectReqMapper expProjectReqMapper;
    @Autowired
    private FlowAuditdetailMapper flowAuditdetailMapper;
    @Autowired
    private BaseModifyDetailServiceImpl baseModifyDetailService;
    @Autowired
    private BaseOwnerCommitteeMapper baseOwnerCommitteeMapper;

    /**
     * 项目审核
     *
     * @param businessId 审核ID
     * @param opinion    审批意见
     * @param mark       审批通过 audit_pass
     *                   审批否决 audit_veto
     *                   审批退回 audit_back
     * @return 操作结果
     */
    @Transactional(rollbackFor = {RuntimeException.class, Exception.class})
    @Override
    public CommonResult audit(String businessId, String opinion, String mark, String proDate, String operNo) {
        if (mark.equals(String.valueOf(NumberEnum.ZERO.getNum()))) {
            mark = "audit_veto";
        } else {
            mark = "audit_pass";
        }
        //返回审批状态
        String auditState = flowAddAuditUtil.audit(businessId, opinion, mark,operNo);
        if (StringUtils.isBlank(auditState)) {
            throw new CourseException(ShiroResultEnum.FAIL.getCode(), MessageEnum.FAIL.getMsg());
        }
        ExpProjectReq expProjectReq = new ExpProjectReq();
        expProjectReq.setId(businessId);
        //修改exp_project_req表的审核状态
        if (NumStrEnum.TWO.getNum().equals(auditState)) {
            //项目终审通过
            expProjectReq.setFlowState(String.valueOf(NumberEnum.TWO.getNum()));
            expProjectReq.setStep(String.valueOf(NumberEnum.FOUR.getNum()));
            //终审通过时间
            proDate = LocalDate.now().toString();
            expProjectReq.setProDate(proDate);
        } else if (NumStrEnum.THREE.getNum().equals(auditState)) {
            //项目审核不通过
            expProjectReq.setFlowState(String.valueOf(NumberEnum.THREE.getNum()));
            expProjectReq.setStep(String.valueOf(NumberEnum.FIVE.getNum()));
        }
        if (StringUtils.isNotEmpty(expProjectReq.getFlowState())) {
            int num = expProjectReqMapper.audit(expProjectReq);
            if (num == NumberEnum.ZERO.getNum()) {
                throw new CourseException(ShiroResultEnum.FAIL.getCode(), MessageEnum.FAIL.getMsg());
            }
        }
        return CommonResult.ResponseResult(NumberEnum.ONE.getNum());
    }

    /**
     * 外网查询应急项目审批列表
     *
     * @param page          分页实体
     * @param expProjectReq 查询实体
     * @return 列表
     */
    @Override
    public PageInfo<ExpProjectReq> getEmergencyAuditSearchList(Page page, ExpProjectReq expProjectReq) {
        //根据登录账号，查询业委会或者居委会管理的小区
        String regionId = baseOwnerCommitteeMapper.getRegionId(expProjectReq.getOperNo());
        List<ExpProjectReq> list = new ArrayList<>();
        if (StringUtils.isNotBlank(regionId)) {
            //查询待审核的项目ID
            Map<String, String> map = flowUtil.getBussIdAndZoneCodeByOperNo(expProjectReq.getOperNo(), FlowTypeEnum.INDUSTRY_COMMITTEE_EMERGENCY.getType() + "," + FlowTypeEnum.NEIGHBORHOOD_COMMITTEE_EMERGENCY.getType());
            if (map != null) {
                String id = map.get("id");
                expProjectReq.setId(id);
                expProjectReq.setRegionId(regionId);
                PageHelper.startPage(page.getPageNum(), page.getPageSize());
                //查询待审核列表
                list = expProjectReqMapper.selectWaitAuditProject(expProjectReq);
            }
        }
        return new PageInfo<>(list);
    }

    /**
     * 项目审核查询列表
     *
     * @param page  分页实体
     *              pageNum 页码
     *              pageSize 每页条数
     * @param fuzzy 模糊查询字段
     * @return 审核查询列表
     */
    @Override
    public PageInfo<ExpProjectReq> getAuditSearchList(Page page, String fuzzy) {
        List<ExpProjectReq> list = new ArrayList<>();
        //一般使用审批集合
        List<ExpProjectReq> projectReqList = getAuditList(FlowTypeEnum.PROJECT_REQ.getType(), fuzzy);
        //内网应急使用审批集合
        List<ExpProjectReq> intranetList = getAuditList(FlowTypeEnum.INTRANET_EMERGENCY.getType(), fuzzy);
        //外网业委会应急使用审批集合
        List<ExpProjectReq> industryList = getAuditList(FlowTypeEnum.INDUSTRY_COMMITTEE_EMERGENCY.getType(), fuzzy);
        //外网居委会应急使用审批集合
        List<ExpProjectReq> neighborhoodList = getAuditList(FlowTypeEnum.NEIGHBORHOOD_COMMITTEE_EMERGENCY.getType(), fuzzy);
        list.addAll(projectReqList);
        list.addAll(intranetList);
        list.addAll(industryList);
        list.addAll(neighborhoodList);
        List pageList = baseModifyDetailService.pageAll(page, list);
        PageInfo<ExpProjectReq> pageInfo = new PageInfo<>(pageList);
        return pageInfo;
    }

    /**
     * 查询各种类型的待审批列表
     *
     * @param flowType 审批类别
     * @param fuzzy    模糊查询字段
     * @return 审批列表
     */
    private List<ExpProjectReq> getAuditList(String flowType, String fuzzy) {
        Map<String, String> map = flowUtil.getBussIdAndZoneCodeByOperNo(flowType);
        List<ExpProjectReq> list = new ArrayList<>();
        if (null != map) {
            String id = map.get("id");
            String zoneCode = map.get("zoneCode");
            ExpProjectReq expProjectReq = new ExpProjectReq();
            expProjectReq.setId(id);
            expProjectReq.setZoneCode(zoneCode);
            expProjectReq.setFuzzy(fuzzy);
            list = expProjectReqMapper.selectWaitAuditProject(expProjectReq);
        }
        for (ExpProjectReq expProjectReq : list) {
            expProjectReq.setFlowType(flowType);
        }
        return list;
    }

    /**
     * 项目审核意见查询
     *
     * @param businessId 审核ID
     * @return 操作结果
     */
    @Override
    public List<FlowAuditdetail> getCurrentAuditdetailList(String businessId) {
        return flowAuditdetailMapper.getCurrentAuditdetailList(businessId, FlowTypeEnum.PROJECT_REQ.getType());
    }
}
